﻿#region Using Statements

using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Web.UI.ControlDesign;

#endregion

namespace Sitefinity.Contrib.SocialWidgets.SocialShareBar
{
    [ControlDesigner(typeof(SocialShareBarDesigner)), PropertyEditorTitle("Social Bookmark Bar")]
    public partial class SocialShareBarWidget : UserControl
    {
        #region Backing Fields

        //we default everything to true so that you only need to disable what you don't want when embedding this in a widget template.

        private bool _showDelicious = true;
        private bool _showDigg = true;
        private bool _showEmail = true;
        private bool _showFacebook = true;
        private bool _showFurl = true;
        private bool _showReddit = true;
        private bool _showStumble = true;
        private bool _showTwitter = true;
        private string _untitledPage = "untitled page";

        #endregion

        #region Public Properties

        public bool ShowDelicious
        {
            get { return _showDelicious; }
            set { _showDelicious = value; }
        }

        public bool ShowDigg
        {
            get { return _showDigg; }
            set { _showDigg = value; }
        }

        public bool ShowReddit
        {
            get { return _showReddit; }
            set { _showReddit = value; }
        }

        public bool ShowStumble
        {
            get { return _showStumble; }
            set { _showStumble = value; }
        }

        public bool ShowFacebook
        {
            get { return _showFacebook; }
            set { _showFacebook = value; }
        }

        public bool ShowFurl
        {
            get { return _showFurl; }
            set { _showFurl = value; }
        }

        public bool ShowTwitter
        {
            get { return _showTwitter; }
            set { _showTwitter = value; }
        }

        public bool ShowEmail
        {
            get { return _showEmail; }
            set { _showEmail = value; }
        }

        public string UntitledPageTitle
        {
            get { return _untitledPage; }
            set { _untitledPage = value; }
        }

        #endregion

        #region Constants

        private const string DeliciousUrl = "http://del.icio.us/post?url={0}&amp;title={1}";

        private const string DiggUrl = "http://digg.com/submit?url={0}&amp;title={1}";

        private const string RedditUrl = "http://www.reddit.com/submit?url={0}&amp;title={1}";

        private const string StumbleUrl = "http://www.stumbleupon.com/submit?url={0}&amp;title={1}";

        private const string FaceBookUrl = "http://www.facebook.com/sharer.php?u={0}&amp;t={1}";

        private const string FurlUrl = "http://www.furl.net/storeIt.jsp?u={0}&amp;title={1}";

        private const string TwitterUrl = "";

        private const string EmailUrl = "mailto://?subject={0}&amp;title={1}";

        #endregion

        private string LinkAddressBuilder(string urlString, string className, string urlTitle, bool generateBlankUrl = false)
        {
            var szUrlTranslated = generateBlankUrl
                                      ? "#"
                                      : string.Format(urlString, Server.UrlEncode(Request.Url.AbsoluteUri),
                                                      Server.UrlEncode(Parent.Page.Title ?? _untitledPage));
            var szOutput = new StringBuilder();

            szOutput.AppendFormat("<li class='{0}' onclick=\"javascript: window.open($(this).find(':first-child').attr('href'), '_blank');\">", className);
            szOutput.AppendFormat("<a href='{0}' target='_blank' title='{1}'></a>", szUrlTranslated, urlTitle);
            szOutput.Append("</li>");

            return szOutput.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            //if in edit mode then we don't want to give them the actual link.
                if (_showDelicious)
                    litDelicious.Text = LinkAddressBuilder(DeliciousUrl, "barlink_delicious", "del.icio.us",
                                                           this.IsDesignMode());
                if (_showDigg)
                    litDigg.Text = LinkAddressBuilder(DiggUrl, "barlink_digg", "Digg This", this.IsDesignMode());

                if (_showReddit)
                    litReddit.Text = LinkAddressBuilder(RedditUrl, "barlink_reddit", "Reddit", this.IsDesignMode());
                
                if (_showStumble)
                    litStumble.Text = LinkAddressBuilder(StumbleUrl, "barlink_stumble", "StumbleUpon", this.IsDesignMode());
                
                if (_showFacebook)
                    litFacebook.Text = LinkAddressBuilder(FaceBookUrl, "barlink_facebook", "Facebook", this.IsDesignMode());
                
                if (_showFurl)
                    litFurl.Text = LinkAddressBuilder(FurlUrl, "barlink_furl", "Furl", this.IsDesignMode());

                if (_showTwitter)
                    litTwitter.Text = LinkAddressBuilder(TwitterUrl, "barlink_twitter", "Twitter", this.IsDesignMode());

                if (_showEmail)
                    litEmail.Text = LinkAddressBuilder(EmailUrl, "barlink_email", "Email This", this.IsDesignMode());
            
        }
    }
}